<?php

/**
 * This is the model class for table "{{groups_comments}}".
 *
 * The followings are the available columns in table '{{groups_comments}}':
 * @property integer $id
 * @property string $text
 * @property integer $group_id
 * @property integer $user_id
 * @property string $crdate
 * @property integer $parent_id
 */
class GroupsComments extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return GroupsComments the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{groups_comments}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('group_id, user_id, crdate', 'required'),
			array('group_id, user_id, parent_id', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, text, group_id, user_id, crdate, parent_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user_info'=>array(self::BELONGS_TO, 'Users', 'user_id'),
			'likes'=>array(self::HAS_MANY, 'GroupsCommentsLikes', 'group_comment_id'),
			'countLikes'=>array(self::STAT, 'GroupsCommentsLikes', 'group_comment_id'),
			'childComments'=>array(self::HAS_MANY, 'GroupsComments', 'parent_id',
				'order' => 'childComments.id ASC',
			),
			'countChildComments'=>array(self::STAT, 'GroupsComments', 'parent_id'),	
			'images'=>array(self::HAS_MANY, 'GroupsCommentsPhotos', 'group_comment_id',
				'order' => 'images.id DESC',
			),	
			'musics'=>array(self::HAS_MANY, 'GroupsCommentsMusics', 'group_comment_id',
				'order' => 'musics.id DESC',
			),	
			'videos'=>array(self::HAS_MANY, 'GroupsCommentsVideos', 'group_comment_id',
				'order' => 'videos.id DESC',
			),													
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'text' => 'Text',
			'group_id' => 'Group',
			'user_id' => 'User',
			'crdate' => 'Crdate',
			'parent_id' => 'Parent',
		);
	}

	protected function beforeDelete()
	{
		if(parent::beforeDelete())
		{
			GroupsCommentsLikes::model()->deleteAll(
				'group_comment_id=:comment_id',
				array(':comment_id'=>$this->id)
			);
			
			GroupsCommentsMusics::model()->deleteAll(
				'group_comment_id=:group_comment_id',
				array(':group_comment_id'=>$this->id)
			);
			GroupsCommentsPhotos::model()->deleteAll(
				'group_comment_id=:group_comment_id',
				array(':group_comment_id'=>$this->id)
			);
			GroupsCommentsVideos::model()->deleteAll(
				'group_comment_id=:group_comment_id',
				array(':group_comment_id'=>$this->id)
			);
			GroupsComments::model()->deleteAll(
				'parent_id=:parent_id',
				array(':parent_id'=>$this->id)
			);
			return true;
		}
		else
			return false;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('text',$this->text,true);
		$criteria->compare('group_id',$this->group_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('crdate',$this->crdate,true);
		$criteria->compare('parent_id',$this->parent_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}